'''
@Project ：dataProcessing.py 
@File    ：main.py
@IDE     ：PyCharm 
@Author  ：子协
@Date    ：2025-07-08 22:32 
'''
import sys
import json
import time
from myemail import email
from my12306 import Railway12306Client,ErrorCode

# 读取json配置文件
def ReadFromJSON(file_path):
    with open(file_path, mode='r', encoding='utf-8') as file:
        config = json.load(file)
    return config

if __name__ == '__main__':
    config = ReadFromJSON(r"./config.json")
    from_station = config['from_station']
    to_station = config['to_station']
    date = config['date']
    hours_before = config['hours_before']
    chair_type_list = config['chair_type_list']
    train_number_list = config['train_number_list']
    from_email_name = config['from_email_name']
    from_email_account = config['from_email_account']
    from_email_token = config['from_email_token']
    to_email_name = config['to_email_name']
    to_email_account = config['to_email_account']
    email_topic = config['email_topic']

    myemail = email(from_email_name, from_email_account, from_email_token)
    my12306 = Railway12306Client()

    # 获取所有车站三字码
    my12306.GetALLCode()
    # 获取起点车站三字码
    from_station_code = my12306.GetCodeByName(from_station)
    # 获取终点车站三字码
    to_station_code = my12306.GetCodeByName(to_station)
    if from_station_code == ErrorCode.ERR_3CodeNotExit or to_station_code == ErrorCode.ERR_3CodeNotExit:
        print("错误:未找到车站信息!")
        sys.exit(-1)
    while True:
        res = my12306.GetLeftTicketURL()
        if res != ErrorCode.OK:
            print("错误:获取余票查询接口URL失败")
            break
        res = my12306.GetAllTrains(from_station_code, to_station_code, date)
        if res != ErrorCode.OK:
            print("错误：获取所有车次信息失败")
            break
        res = my12306.GetTrainByTrainNumber(train_number_list)              # 获取指定车次信息
        if res != ErrorCode.OK:
            print("错误：车次不存在")
            break
        left_ticket_list = my12306.GetLeftTicketByChairType(chair_type_list)  # 获取指定车次指定座位类型的余票信息
        print(left_ticket_list)
        res = my12306.HasAvailableTickets(left_ticket_list)  # 检查是否有票
        if res != ErrorCode.OK:                              # 无票  检查是否到达截至时间
            res = my12306.CheckDateReminder(left_ticket_list, date, hours_before)
            if res != ErrorCode.OK:                          # 有车次到达了截至时间
                msg = f"车次:{my12306.timeout_trains_list[0]} 已到达截至时间,停止查询"
                print(msg)
                train_number_list.remove(my12306.timeout_trains_list[0])  # 移除到达截至时间的车次
                # print(train_number_list)
                res = myemail.login()
                if res != 0:
                    print("错误：邮箱登录失败")
                    break
                print("登录成功")
                res = myemail.sendText(to_email_name, to_email_account, email_topic[1], msg)
                if res != 0:
                    print("错误：发送邮箱失败")
                    break
                print("发送成功")
                myemail.quit()
                if len(train_number_list) == 0:                     # 所有车次均到达截至时间
                    msg = f"很遗憾,所有车次均已到达截至时间>.<"
                    print(msg)
                    res = myemail.login()
                    if res != 0:
                        print("错误：邮箱登录失败")
                        break
                    print("登录成功")
                    res = myemail.sendText(to_email_name, to_email_account, email_topic[1], msg)
                    if res != 0:
                        print("错误：发送邮箱失败")
                        break
                    print("发送成功")
                    myemail.quit()
                    break
            else:                                               # 所有车次都没有达到截至时间
                print("还是没票>.<,再等等吧")
        else:                   # 有票  发送邮件通知
            msg = f"车次:{my12306.available_tickets_list[0]} | 出发时间:{my12306.available_tickets_list[1]}"
            for chair_type, tickets in zip(chair_type_list, my12306.available_tickets_list[2]):
                msg += f" | {chair_type}:{tickets}"
            print("有票了! " + msg)
            res = myemail.login()
            if res != 0:
                print("错误：邮箱登录失败")
                break
            print("登录成功")
            res = myemail.sendText(to_email_name, to_email_account, email_topic[0], msg)
            if res != 0:
                print("错误：发送邮箱失败")
                break
            print("发送成功")
            myemail.quit()
            break
        time.sleep(60)  # 暂停1分钟